<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
$tabs=array(
	array('op'=>'edit','title'=>'添加商品'),
	
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
//上架商品
if($_RQ['op']=='list'){
	$params=array('status <>'=>3,'merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','displayorder','title','thumb','cate','type','goodstype','price','credit','money','status','isrecommand','total','is_moretype','creditmore','createtime');
	$list=pdo_getslice('jy_creditshop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']='jy_creditshop/goods_detail//id='.$item['id'];
			if($item['is_moretype']){
				$item['creditmore']=$item['creditmore']?json_decode($item['creditmore'],true):array();
				$item['credit']=$item['creditmore'][0]['credit'];
			}
		}
	}	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：如果商品类型是红包的情况下，请确保已经设置好领红包的商户参数：移步：站点设置--支付参数--上传支付证书（如果要实现退款，领红包功能，必须上传证书！）','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'displayorder','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'thumb','title'=>'商品图片','type'=>'image'),
			array('name'=>'goodstype','title'=>'商品类型','type'=>'radio','options'=>'goodstype'),
			array('name'=>'type','title'=>'活动类型','type'=>'radio','options'=>'type'),			
			array('name'=>'credit','title'=>'消耗','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'url','title'=>'复制短链','type'=>'copy'),
			array('name'=>'isrecommand','title'=>'推荐','type'=>'radio','options'=>'isrecommand'),
		)),
		'search_one'=>array('name'=>'title','title'=>'商品名称','add'=>false),
	);
	$return['isrecommand']=array('0'=>'不推荐','1'=>'推荐');
	$return['goodstype']=$this->goodstype;
	$return['type']=array('0'=>'兑换','1'=>'抽奖');
	$return['status']=array('0'=>'暂停','1'=>'启动','3'=>'已删除');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_creditshop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['timestart']=date('Y-m-d h:i',$item['timestart']);
		$item['timeend']=date('Y-m-d h:i',$item['timeend']);
		//获取分类
		$cname=pdo_getcolumn('jy_creditshop_category',array('id'=>$item['cate']),'name');
		$item['cid']=array('name'=>$cname,'cate'=>$item['cate']);	
		$specs_list=pdo_getall('jy_creditshop_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','displayorder asc');		
		if($specs_list){
			foreach($specs_list as &$row){
				$row['options']=pdo_getall('jy_creditshop_spec_item',array('specid'=>$row['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','displayorder asc');
			}
			$item['specs']['specs']=$specs_list;
		}
		$option_list=pdo_getall('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','total','credit','price','goodssn','productsn','weight'),'','displayorder asc');
		if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_creditshop_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_creditshop_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['num']=$i['total'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			$item['endtime']=date('Y-m-d',$item['endtime']);
		}
		$item['specs']['tds']=$option_list;
	}else{
		$item=array('is_moretype'=>0);
	}
	$return['item']=$item;
	$return['item']['storeids']=json_decode($return['item']['storeids'],true);
	$return['item']['creditmore']=$return['item']['creditmore']?json_decode($return['item']['creditmore'],true):array();
	$return['item']['param']=json_decode($return['item']['param'],true);
	$return['item']['images']=json_decode($return['item']['images'],true);
	$return['item']['istime_time']=json_decode($return['item']['istime_time'],true);
	$return['item']['isdiscount_time']=json_decode($return['item']['isdiscount_time'],true);
	$return['item']['presellonlinetime']=json_decode($return['item']['presellonlinetime'],true);
	$return['item']['presellsendtime']=json_decode($return['item']['presellsendtime'],true);
	$return['item']['fields']=$fields;
	$return['item']['pcd']=json_decode($return['item']['pcd']);
	$return['pcd']=cfc('tools')->pcd();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'displayorder','title'=>'排序','type'=>'string','help'=>'数字越大，排名越靠前'),
					array('name'=>'goodstype','title'=>'商品类型','type'=>'radio','options'=>'goodstype'),
					//array('type'=>'choose_up','title'=>'选择商品','name'=>'goodsid','action'=>'get_goods','choose_title'=>'选择商品','help'=>'','show'=>array('goodstype'=>0)),
					array('type'=>'choose','title'=>'选择优惠券','name'=>'couponid','action'=>'jy_weishop/coupon.get_coupon/','choose_title'=>'选择优惠券','show'=>array('goodstype'=>1)),
					array('name'=>'grant1','title'=>'获得余额','type'=>'unitstring','unit'=>'元','show'=>array('goodstype'=>2)),
					array('name'=>'grant1','title'=>'获得'.$this->xunitype[4].'个数','type'=>'unitstring','unit'=>'个','show'=>array('goodstype'=>4)),
					array('name'=>'grant1','title'=>'获得'.$this->xunitype[5].'个数','type'=>'unitstring','unit'=>'个','show'=>array('goodstype'=>5)),
					array('name'=>'grant1','title'=>'获得'.$this->xunitype[6].'个数','type'=>'unitstring','unit'=>'个','show'=>array('goodstype'=>6)),
					array('title'=>'红包总发放金额','name1'=>'packetmoney','title1'=>'总额','type'=>'string_two','unit1'=>'元','name2'=>'surplusmoney','title2'=>'剩余','unit2'=>'元','show'=>array('goodstype'=>3)),
					array('title'=>'红包兑换限制（剩余金额小于该值时停止兑换）','name'=>'packetlimit','type'=>'unitstring','unit'=>'元','show'=>array('goodstype'=>3)),
					array('title'=>'红包个数','name1'=>'packettotal','title1'=>'红包','type'=>'string_two','unit1'=>'个','name2'=>'packetsurplus','title2'=>'剩余','unit2'=>'个','show'=>array('goodstype'=>3)),
					array('name'=>'packettype','title'=>'红包类型','type'=>'radio','options'=>'packettype','show'=>array('goodstype'=>3)),
					array('name'=>'minpacketmoney','title'=>'每个红包最低金额','type'=>'unitstring','unit'=>'元','show'=>array('goodstype'=>3,'packettype'=>0),'help'=>'金额需大于1元'),
					array('name'=>'maxpacketmoney','title'=>'每个红包最高金额','type'=>'unitstring','unit'=>'元','show'=>array('goodstype'=>3,'packettype'=>0),'help'=>'金额需大于每个红包最低金额'),
					array('name'=>'grant2','title'=>'每个红包','type'=>'unitstring','unit'=>'元','show'=>array('goodstype'=>3,'packettype'=>1),'help'=>'金额需大于1元'),
					array('name'=>'title','title'=>'商品名称','type'=>'string'),
					array('name'=>'thumb','title'=>'商品图片','type'=>'image'),
					array('name'=>'images','title'=>'商品幻灯片','type'=>'images'),
					array('name'=>'cate','title'=>'分类','type'=>'select','options'=>'cid','option_title'=>'name'),
					array('name'=>'price','title'=>'商品原价','type'=>'string'),
					array('name'=>'set_credit1','title'=>'赠送积分数','type'=>'unitstring','unit'=>'个','help'=>'填写后，下单支付成功，送积分！'),
					array('title'=>'兑换限制','name1'=>'chance','title1'=>'每人共','type'=>'string_two','unit1'=>'次','name2'=>'chanceday','title2'=>'每天提供','unit2'=>'份'),
					array('name'=>'is_moretype','title'=>'多种兑换方式？','type'=>'switch'),
					array(
							'type'=>'array',
							'title'=>'消耗方式',
							'name'=>'creditmore',
							'show'=>array('is_moretype'=>1),
							'add_tip'=>'添加一个',
								'list'=>array(
									array('name'=>'credit_type','title'=>'积分类型','type'=>'radio','options'=>'credit_types'),
									array('type'=>'string','name'=>'credit','title'=>'积分额度'),
									array('type'=>'string','name'=>'money','title'=>'+金额'),
								),
								'details'=>array(
									array('title'=>'活动消耗','name1'=>'credit','title1'=>'消耗','type'=>'string_tworadio','unit1'=>'credit_type','unit1title'=>'默认积分','unit1options'=>'credit_types','name2'=>'money','title2'=>'+金额','unit2'=>'元'),
								)
					),
					array('title'=>'消耗方式','name1'=>'credit','title1'=>'消耗','type'=>'string_tworadio','unit1'=>'credit_type','unit1title'=>'默认积分','unit1options'=>'credit_types','name2'=>'money','title2'=>'+金额','unit2'=>'元','show'=>array('is_moretype'=>0)),
				    array('name'=>'isrecommand','title'=>'推荐','type'=>'switch'),
					array('name'=>'salenum','title'=>'兑换次数','type'=>'unitstring','unit'=>'次'),
				    array('name'=>'istime','title'=>'限时','type'=>'switch'),
					array('type'=>'checkbox','title'=>'限制星期','name'=>'work_time','help'=>'','options'=>'work_time','show'=>array('istime'=>1)),
				    array('name'=>'timestart','title'=>'限时开始时间','type'=>'time','show'=>array('istime'=>1)),
				    array('name'=>'timeend','title'=>'限时结束时间','type'=>'time','show'=>array('istime'=>1)),
					array('name'=>'dispatchtype','title'=>'运费设置','type'=>'radio','options'=>'dispatchtype','show'=>array('goodstype'=>0)),
					array('name'=>'dispatchid','title'=>'默认模板','type'=>'select','options'=>'dispatchid','show'=>array('goodstype'=>0,'dispatchtype'=>0)),
					array('name'=>'dispatch','title'=>'统一运费','type'=>'string','show'=>array('goodstype'=>0,'dispatchtype'=>1)),
					array('name'=>'status','title'=>'活动状态','type'=>'radio','options'=>'status'),
				)
			),
			array(
				'title'=>'库存/规格',
				'show'=>array('goodstype'=>0),
				'list'=>array(
					array('name'=>'goodssn','title'=>'编码','type'=>'string','help'=>''),
					array('name'=>'productsn','title'=>'条码','type'=>'string'),
					array('name'=>'weight','title'=>'重量','type'=>'unitstring','unit'=>'克','help'=>''),
					array('name'=>'total','title'=>'库存','type'=>'string'),
					array('name'=>'specs','title'=>'规格','type'=>'spec_diy','fields'=>array(
						array('name'=>'total','title'=>'库存','type'=>'string'),
						array('name'=>'credit','title'=>'积分','type'=>'string'),
						array('name'=>'price','title'=>'金额','type'=>'string'),
						array('name'=>'goodssn','title'=>'编码','type'=>'string'),
						array('name'=>'productsn','title'=>'条码','type'=>'string'),
						array('name'=>'weight','title'=>'重量（克）','type'=>'string'),	
						array('name'=>'thumb','title'=>'图片','type'=>'image'),	
					)),
				)
			),
			array(
				'title'=>'详情',
				'list'=>array(
					array('name'=>'goodsdetail','title'=>'商品简介','type'=>'richtext','help'=>''),
					array('name'=>'detailshow','title'=>'是否单独开启商品兑换流程','type'=>'switch','help'=>''),
					array('name'=>'detail','title'=>'兑换流程','type'=>'richtext','show'=>array('detailshow'=>'1'),'help'=>''),
					array('name'=>'noticedetailshow','title'=>'是否单独开启商品注意事项','type'=>'switch','help'=>''),
					array('name'=>'noticedetail','title'=>'商品注意事项','type'=>'richtext','show'=>array('noticedetailshow'=>'1'),'help'=>''),
				)
			),
			array(
				'title'=>'抽奖设置',
				'list'=>array(
					array('name'=>'type','title'=>'是否开启抽奖','type'=>'switch','help'=>'保存后请勿更改'),
					array('name'=>'rate','title'=>'中奖率','type'=>'unitstring','unit'=>'%','show'=>array('type'=>'1'),'help'=>''),
				)
			),
		/*	array(
				'title'=>'会员特权',
				'list'=>array(
					array('name'=>'showlevels','title'=>'会员等级浏览权限','type'=>'select','options'=>'showlevels','help'=>''),
					array('name'=>'buylevels','title'=>'会员等级购买权限','type'=>'select','options'=>'buylevels','help'=>''),
					array('name'=>'showgroups','title'=>'会员组浏览权限','type'=>'select','options'=>'showgroups','help'=>''),
					array('name'=>'buygroups','title'=>'会员组购买权限','type'=>'select','options'=>'buygroups','help'=>''),
				)
			),*/
			array(
				'title'=>'分享关注',
				'list'=>array(
					array('name'=>'followneed','title'=>'参加强制关注','type'=>'radio','options'=>'followneed','help'=>''),
					array('name'=>'followtext','title'=>'关注提示','type'=>'string','show'=>array('followneed'=>'1'),'help'=>''),
					array('name'=>'share_title','title'=>'分享标题','type'=>'string','help'=>''),
					array('name'=>'share_icon','title'=>'分享图标','type'=>'image','help'=>''),
					array('name'=>'share_desc','title'=>'分享描述','type'=>'text','help'=>'如果不填写，则使用商品副标题，如商品副标题为空则使用店铺名称'),
				)
			),
			array(
				'title'=>'商家管理',
				'list'=>array(
					array('type'=>'choose','title'=>'选择通知人','name'=>'noticemid','action'=>'core/member.get_member/','choose_title'=>'选择通知人'),
					array('name'=>'subtitle','title'=>'提供商家','type'=>'string','help'=>''),
					array('name'=>'subdetail','title'=>'商家介绍','type'=>'richtext','help'=>''),
					
			)
		),
			array(
				'title'=>'线下核销',
				'list'=>array(
					array('name'=>'isverify','title'=>'支持核销','type'=>'switch','help'=>''),
					array('name'=>'storeids','title'=>'线下兑换门店选择','type'=>'choose_many','show'=>array('isverify'=>1),'action'=>'jy_weishop/store.get_store/many/','choose_title'=>'选择门店','help'=>'如果不选择门店，则此商品支持在所有门店兑换'),
					array('name'=>'verifytype','title'=>'核销方式','type'=>'radio','options'=>'verifytype','show'=>array('isverify'=>1),'help'=>'按订单核销： 不管够买多少 一次核销完成；按次核销： 一个消费码使用多次'),
					array('name'=>'verifynum','title'=>'核销次数','type'=>'string','show'=>array('verifytype'=>1,'isverify'=>1),'help'=>''),
					array('name'=>'isendtime','title'=>'核销限时','type'=>'radio','options'=>'isendtime','show'=>array('isverify'=>1),'help'=>''),
					array('name'=>'usetime','title'=>'自动使用期限','type'=>'string','show'=>array('isendtime'=>0,'isverify'=>1),'help'=>'商品购买X天后自动使用，自动使用后无法退款，设置为0则没有限制'),
					array('name'=>'endtime','title'=>'使用有效期至','type'=>'time','show'=>array('isendtime'=>1,'isverify'=>1),'help'=>''),
			)
		),
		)
	);
	//获取分类
	$saletype=array('1'=>'推荐','2'=>'限时购买');
	$dispatchtype=array('0'=>'默认模板','1'=>'统一运费');
	$followneed=array('0'=>'否','1'=>'是');
	$noticetype=array('1'=>'付款通知','2'=>'买家收货通知');
	$verifytype=array('0'=>'按订单核销','1'=>'按次核销');
	$diyformtype=array('0'=>'关闭','1'=>'自定义','2'=>'使用模板');
	$return['storeids']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['dispatchid']=pdo_getall('jy_weishop_dispatch',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['saletype']=$saletype;
	$return['dispatchtype']=$dispatchtype;
	$return['totalcnf']=$totalcnf;
	$return['followneed']=$followneed;
	$return['noticetype']=$noticetype;
	$return['verifytype']=$verifytype;
	$return['diyformtype']=$diyformtype;
	
	$return['cid']=pdo_getall('jy_creditshop_category',array('uniacid'=>$_SESSION['uniacid'],'enabled'=>1,'merchid'=>$_SESSION['onemerchid']),array('id','name'),'id');
	$return['isrecommand']=array('0'=>'不推荐','1'=>'推荐');
	$return['goodstype']=$this->goodstype;
	$return['type']=array('0'=>'兑换','1'=>'抽奖');
	$return['status']=array('0'=>'暂停','1'=>'启动');
	$return['packettype']=array('0'=>'随机金额','1'=>'固定金额');
	$return['isendtime']=array('0'=>'指定时间核销','1'=>'限时核销');
	$return['work_time']=array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
	$return['credit_types']=credit_types();
	foreach($return['credit_types'] as $k=>$cv){
		if($k==2){
			unset($return['credit_types'][$k]);
		}
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['goodstype']==3 && $postdata['packettype']==1 && (!$postdata['grant2'] || $postdata['grant2']<1)){
		exi('红包类型时，固定金额情况下，红包金额不能小于1','error');
	}
	if($postdata['goodstype']==3 && $postdata['packettype']!=1 && (!$postdata['minpacketmoney'] || $postdata['minpacketmoney']<1 || !$postdata['maxpacketmoney'] || $postdata['maxpacketmoney']<1)){
		exi('红包类型时，随机金额情况下，最小金额和最大金额不能小于1','error');
	}
	$data=array(
		'displayorder'=>intval($postdata['displayorder']),
		'goodstype'=>intval($postdata['goodstype']),
		'couponid'=>intval($postdata['couponid']),
		'grant1'=>intval($postdata['grant1']),
		'packetmoney'=>intval($postdata['packetmoney']),
		'surplusmoney'=>intval($postdata['surplusmoney']),
		'packetlimit'=>intval($postdata['packetlimit']),
		'packettotal'=>intval($postdata['packettotal']),
		'packetsurplus'=>intval($postdata['packetsurplus']),
		'packettype'=>intval($postdata['packettype']),
		'minpacketmoney'=>intval($postdata['minpacketmoney']),
		'maxpacketmoney'=>intval($postdata['maxpacketmoney']),
		'grant2'=>intval($postdata['grant2']),
		'title'=>trim($postdata['title']),
		'thumb'=>trim($postdata['thumb']),
		'images'=>ijson_encode($postdata['images']),
		'cate'=>intval($postdata['cate']),
		'price'=>$postdata['price'],
		'chance'=>intval($postdata['chance']),
		'chanceday'=>intval($postdata['chanceday']),
		'credit'=>intval($postdata['credit']),
		'money'=>$postdata['money'],
		//'saletype'=>intval($postdata['saletype']),
		'istime'=>intval($postdata['istime']),
		'isrecommand'=>intval($postdata['isrecommand']),
		'timestart'=>strtotime($postdata['timestart']),
		'timeend'=>strtotime($postdata['timeend']),
		'dispatchtype'=>intval($postdata['dispatchtype']),
		'dispatchid'=>intval($postdata['dispatchid']),
		'dispatch'=>intval($postdata['dispatch']),
		'status'=>intval($postdata['status']),		
		//库存
		'goodsid'=>intval($postdata['goodsid']),
		'goodssn'=>trim($postdata['goodssn']),
		'productsn'=>trim($postdata['productsn']),
		'weight'=>intval($postdata['weight']),
		'total'=>intval($postdata['total']),
		
		//详情
		'goodsdetail'=>trim($postdata['goodsdetail']),
		'detail'=>trim($postdata['detail']),
		'noticedetail'=>trim($postdata['noticedetail']),
		'detailshow'=>intval($postdata['detailshow']),
		'noticedetailshow'=>intval($postdata['noticedetailshow']),
		
		//会员特权
		'showlevels'=>intval($postdata['showlevels']),
		'buylevels'=>intval($postdata['buylevels']),
		'showgroups'=>intval($postdata['showgroups']),
		'buygroups'=>intval($postdata['buygroups']),
			
		//分享关注
		'followneed'=>intval($postdata['followneed']),
		'followtext'=>trim($postdata['followtext']),
		'share_title'=>trim($postdata['share_title']),
		'share_icon'=>trim($postdata['share_icon']),
		'share_desc'=>trim($postdata['share_desc']),

		//抽奖设置
		'type'=>intval($postdata['type']),
		'rate'=>intval($postdata['rate']),
		
		//卖家通知
		'noticemid'=>intval($postdata['noticemid']),
		'subtitle'=>trim($postdata['subtitle']),
		'subdetail'=>trim($postdata['subdetail']),

		//核销
		'isverify'=>intval($postdata['isverify']),
		'storeids'=>ijson_encode($postdata['storeids']),
		'verifytype'=>intval($postdata['verifytype']),
		'verifynum'=>intval($postdata['verifynum']),
		'isendtime'=>intval($postdata['isendtime']),
		'usetime'=>intval($postdata['usetime']),
		'endtime'=>strtotime($postdata['endtime']),
		'set_credit1'=>intval($postdata['set_credit1']),
		//
		'credit_type'=>intval($postdata['credit_type']),
		'work_time'=>trim($postdata['work_time']),
		'is_moretype'=>intval($postdata['is_moretype']),
		'creditmore'=>ijson_encode($postdata['creditmore']),
		'salenum'=>intval($postdata['salenum']),
	);	
	$goodsid=intval($postdata['id']);
	if($goodsid){
		pdo_update('jy_creditshop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		pdo_insert('jy_creditshop_goods',$data);
		$goodsid=pdo_insertid();
	}
	//整理商品规格数据
	if($postdata['specs']){
		$spec_ids=array();//接收当前生效的规格ID
		$spec_option_ids=array();//接收当前生效的规格项ID
		$spec_goods_ids=array();//接收当前生效的规格商品ID
		//第一步，整理规格和规格项
		foreach($postdata['specs']['specs'] as $spec_key=>&$spec){
			if(!$spec['id']){				
				//数据库里面没有该规格
				$new_spec=array(
					'uniacid'=>$_SESSION['uniacid'],
					'goodsid'=>$goodsid,
					'displayorder'=>$spec_key,
					'title'=>$spec['title'],
				);
				pdo_insert('jy_creditshop_spec',$new_spec);
				$spec['id']=pdo_insertid();
			}else{
				if($postdata['goodsid']){	
					$new_spec=array(
					'uniacid'=>$_SESSION['uniacid'],
					'goodsid'=>$goodsid,
					'displayorder'=>$spec_key,
					'title'=>$spec['title'],
				);
				pdo_insert('jy_creditshop_spec',$new_spec);
				//print_r($postdata['goodsid'].'ggg');exit;
				$spec['id']=pdo_insertid();
				}else{
					pdo_update('jy_creditshop_spec',array('title'=>$spec['title']),array('id'=>$spec['id']));
				}
			}
			$spec_ids[]=$spec['id'];
			foreach($spec['options'] as $option_key=>&$option){
				if(!$option['id']){
					$new_spec_option=array(
						'uniacid'=>$_SESSION['uniacid'],
						'goodsid'=>$goodsid,
						'specid'=>$spec['id'],
						'title'=>$option['title'],
						'displayorder'=>$option_key,
					);
					pdo_insert('jy_creditshop_spec_item',$new_spec_option);
					$option['id']=pdo_insertid();
				}else{
					if($postdata['goodsid']){
						$new_spec_option=array(
						'uniacid'=>$_SESSION['uniacid'],
						'goodsid'=>$goodsid,
						'specid'=>$spec['id'],
						'title'=>$option['title'],
						'displayorder'=>$option_key,
					);
					pdo_insert('jy_creditshop_spec_item',$new_spec_option);
					$option['id']=pdo_insertid();
					}
					else{
					pdo_update('jy_creditshop_spec_item',array('title'=>$option['title']),array('id'=>$option['id']));
					}
				}
				$spec_option_ids[]=$option['id'];
			}
		}
		
		
		//第二步，整理规格商品数据
		foreach($postdata['specs']['tds'] as $key=>$spec_goods){
			$data=array(
				'displayorder'=>$key,
				'total'=>intval($spec_goods['total']),
				'credit'=>intval($spec_goods['credit']),
				'price'=>price($spec_goods['price']),
				'goodssn'=>trim($spec_goods['goodssn']),
				'productsn'=>trim($spec_goods['productsn']),
				'thumb'=>trim($spec_goods['thumb']),
				'weight'=>intval($spec_goods['weight']),
			);
			if($spec_goods['id']&& !$postdata['goodsid']){
				pdo_update('jy_creditshop_option',$data,array('id'=>intval($spec_goods['id']),'uniacid'=>$_SESSION['uniacid']));
			}else{
				//新的规格商品，根据规格值找到相应规格ID
				$data['specs']=array();
				foreach($postdata['specs']['specs'] as $sp){
					foreach($sp['options'] as $op){
						if($spec_goods[$sp['title']]==$op['title']){
							$data['specs'][]=$op['id'];
							break;
						}
					}
				}
				$data['specs']=implode('_',$data['specs']);
				$data['uniacid']=$_SESSION['uniacid'];
				$data['goodsid']=$goodsid;
				pdo_insert('jy_creditshop_option',$data);
				$spec_goods['id']=pdo_insertid();
			}
			$spec_goods_ids[]=$spec_goods['id'];
		}
		//清理数据库中弃用的规格、规格项、规格商品
		if(!empty($spec_ids)){
			pdo_delete('jy_creditshop_spec',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_ids));
		}
		if(!empty($spec_option_ids)){
			pdo_delete('jy_creditshop_spec_item',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_option_ids));
		}
		if(!empty($spec_goods_ids)){
			pdo_delete('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_goods_ids));
		}
		
		$options = pdo_getall('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price asc');
		$options2 = pdo_getall('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price desc');
		if($options){
			$upgoods =array();
			$upgoods['minmoney'] = $options[0]['price'];
			$upgoods['maxmoney'] = $options2[0]['price'];
			pdo_update('jy_creditshop_goods',$upgoods,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		}
		
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_creditshop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}